using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; namespace RebelHeartEclipse { public class SquadManager : MonoBehaviour { [Header("--------------------CHARACTER OBJS")] [SerializeField] public GameObject[] _prefabCharacterObjs; //Populate in order of CharacterID enum. public Dictionary> _baseCharacterObjDict; [Header("--------------------CHARACTER INDICATOR OBJS")] [SerializeField] public GameObject[] _prefabIndicatorObjs; public Dictionary> _baseIndicatorObjDict; [Header("--------------------BATTLE GROUND GRIDGEN PLUGIN")] [Header("Plugin the grid generator here.")] [SerializeField] public BattleGroundGridGenerator _battleGroundGridGenerator; private CharacterObj _currentPlayerObj; private CharacterObj _currentEnemyObj; private int _banditCount = 10; private int _rogueCount = 10; private int _bowmanCount = 10; private int _knightCount = 10; private Transform _transform; private Vector3 _curPosition; private CameraController _cameraController; private void Awake() => Setup(); private void Start() { } private void Setup() { _transform = this.transform; _cameraController = Camera.main.GetComponent(); PopulateSquadDict(); PopulateSquadIndicators(); SpawnSquad(); } private void PopulateSquadDict() { if (_baseCharacterObjDict == null) _baseCharacterObjDict = new Dictionary>(); _baseCharacterObjDict.Add(CharacterID.Bandit, new List()); _baseCharacterObjDict.Add(CharacterID.Rogue, new List()); _baseCharacterObjDict.Add(CharacterID.Bowman, new List()); _baseCharacterObjDict.Add(CharacterID.Knight, new List()); for (int i = 0; i < _banditCount; i++) { AddCharacterObj(CharacterID.Bandit); } for (int i = 0; i < _rogueCount; i++) { AddCharacterObj(CharacterID.Rogue); } for (int i = 0; i < _bowmanCount; i++) { AddCharacterObj(CharacterID.Bowman); } for (int i = 0; i < _knightCount; i++) { AddCharacterObj(CharacterID.Knight); } } private void AddCharacterObj(CharacterID characterID) { GameObject newGameObject = (GameObject)Instantiate(_prefabCharacterObjs[(int)characterID]); //_baseGridCellObjCached[x, y] = newGameObject.GetComponent(); newGameObject.transform.SetParent(_transform, false); // False to avoid world space recalculations //newGameObject.transform.parent = _transform; newGameObject.SetActive(false); if (_baseCharacterObjDict.TryGetValue(characterID, out List listPlatforms)) listPlatforms.Add(newGameObject); } private GameObject GetCharacterObj(CharacterID characterID) { //*Omitted return null; } private float GetCharacterObjWidth(CharacterID characterID) { float width = 0f; if (_prefabCharacterObjs[(int)characterID] != null) { SpriteRenderer sr = _prefabCharacterObjs[(int)characterID].GetComponent(); if (sr != null) { width = sr.bounds.size.x; } } return width; } private float GetCharacterObjHeight(CharacterID characterID) { float height = 0f; if (_prefabCharacterObjs[(int)characterID] != null) { SpriteRenderer sr = _prefabCharacterObjs[(int)characterID].GetComponent(); if (sr != null) { height = sr.bounds.size.y; } } return height; } private void PopulateSquadIndicators() { if (_baseIndicatorObjDict == null) _baseIndicatorObjDict = new Dictionary>(); _baseIndicatorObjDict.Add(CharacterIndicatorID.Blue_Squad, new List()); _baseIndicatorObjDict.Add(CharacterIndicatorID.Red_Quad, new List()); int maxRedSquad = _banditCount + _rogueCount + _bowmanCount; int maxBlueSquad = _knightCount; for (int i = 0; i < maxRedSquad; i++) { AddIndicatorObj(CharacterIndicatorID.Red_Quad); } for (int i = 0; i < maxBlueSquad; i++) { AddIndicatorObj(CharacterIndicatorID.Blue_Squad); } } private void AddIndicatorObj(CharacterIndicatorID indicatorID) { GameObject newGameObject = (GameObject)Instantiate(_prefabIndicatorObjs[(int)indicatorID]); //_baseGridCellObjCached[x, y] = newGameObject.GetComponent(); newGameObject.transform.SetParent(_transform, false); // False to avoid world space recalculations //newGameObject.transform.parent = _transform; newGameObject.SetActive(false); if (_baseIndicatorObjDict.TryGetValue(indicatorID, out List listPlatforms)) listPlatforms.Add(newGameObject); } private GameObject GetIndicatorObj(CharacterIndicatorID indicatorID) { if (_prefabIndicatorObjs == null) return null; List list; for (int i = 0; i < _baseIndicatorObjDict.Count; i++) { list = _baseIndicatorObjDict[indicatorID]; for (int j = 0; j < list.Count; j++) { if (!_baseIndicatorObjDict[indicatorID][j].activeInHierarchy) { return _baseIndicatorObjDict[indicatorID][j]; } } } return null; } public void SpawnSquad() { int x = 8; int y = 5; for (int i = 0; i < 2; i++) SpawnCharacter(false, CharacterID.Knight, x + i, (y + 3), i, 1); for (int i = 0; i < 2; i++) SpawnCharacter(false, CharacterID.Bowman, x + i, (y + 2), i, 1); for (int i = 0; i < 2; i++) SpawnCharacter(false, CharacterID.Rogue, x + i, (y+1), i, 1); for (int i = 0; i < 3; i++) SpawnCharacter(false, CharacterID.Bandit, (x-1) + i, y, i, 1); SpawnCharacter(true, CharacterID.Knight, (x - 1), 8, 0, 1); SpawnCharacter(true, CharacterID.Knight, (x-2), 8, 0, 5); SpawnCharacter(true, CharacterID.Bowman, (x - 3), 8, 0, 4); SpawnCharacter(true, CharacterID.Bowman, (x - 4), 8, 0, 2); //_cameraController.SetPivotPoint(_battleGroundGridGenerator._baseGridCellObjCached[20, 20].GetCellPosition()); } private void SpawnCharacter(bool isPlayerCharacter, CharacterID characterID, int x, int y, int id, int classLevel) { GameObject obj = GetCharacterObj(characterID); if (obj == null) return; _curPosition = _battleGroundGridGenerator._baseGridCellObjCached[x, y].GetCellPosition(); float heightDiff = GetCharacterObjHeight(characterID) / 2; float widthDiff = GetCharacterObjWidth(characterID) / 2; _curPosition.y += heightDiff - (heightDiff / 4); _curPosition.x += widthDiff / 5; _curPosition.z += widthDiff / 5; obj.transform.position = _curPosition; obj.SetActive(true); heightDiff = heightDiff - (heightDiff / 4); widthDiff = widthDiff / 5; CharacterObj characterObj = obj.GetComponent(); //characterObj.SetID(id); if (characterID == CharacterID.Bandit) { characterObj.InitClassStatus(isPlayerCharacter, CharacterClass.Bandit, classLevel, SpawnIndicator(isPlayerCharacter, _curPosition, widthDiff), id, heightDiff, widthDiff); } if (characterID == CharacterID.Rogue) { characterObj.InitClassStatus(isPlayerCharacter, CharacterClass.Rogue, classLevel, SpawnIndicator(isPlayerCharacter, _curPosition, widthDiff), id, heightDiff, widthDiff); } if (characterID == CharacterID.Bowman) { characterObj.InitClassStatus(isPlayerCharacter, CharacterClass.Bowman, classLevel, SpawnIndicator(isPlayerCharacter, _curPosition, widthDiff), id, heightDiff, widthDiff); } if (characterID == CharacterID.Knight) { characterObj.InitClassStatus(isPlayerCharacter, CharacterClass.Knight, classLevel, SpawnIndicator(isPlayerCharacter, _curPosition, widthDiff), id, heightDiff, widthDiff); } _battleGroundGridGenerator._baseGridCellObjCached[x, y].AssignCharacterObj(characterObj); } private GameObject SpawnIndicator(bool isPlayerCharacter, Vector3 spawnPos, float width) { CharacterIndicatorID indicatorID = CharacterIndicatorID.Blue_Squad; if (isPlayerCharacter) indicatorID = CharacterIndicatorID.Blue_Squad; if (!isPlayerCharacter) indicatorID = CharacterIndicatorID.Red_Quad; GameObject obj = GetIndicatorObj(indicatorID); //spawnPos.x -= width / 5; spawnPos.y -= 0.1f; spawnPos.x -= 0.09f; spawnPos.z -= 0.09f; obj.transform.position = spawnPos; obj.SetActive(true); return obj; } public void SetCurrentPlayerObj(ref CharacterObj playerObj) => _currentPlayerObj = playerObj; public void SetCurrentEnemyObj(ref CharacterObj enemyObj) => _currentEnemyObj = enemyObj; public CharacterObj GetCurrentPlayerObj() => _currentPlayerObj; public CharacterObj GetCurrentEnemyObj() => _currentEnemyObj; } }